package ssm.controller;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import ssm.common.Task;


@Controller
public class TestController {



    private SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();

    @RequestMapping("/start")
    public void add() throws ClassNotFoundException, SchedulerException, InterruptedException {

        Scheduler sche = gSchedulerFactory.getScheduler();
        Task.addJob(sche, "测试", Class.forName("ssm.common.Hello"), "0/1 * * * * ?");

    }

    @RequestMapping("/update")
    public void update() throws ClassNotFoundException, SchedulerException {
        Scheduler sche = gSchedulerFactory.getScheduler();
        Task.modifyJobTime(sche, "测试",  "0/5 * * * * ?");
    }



    @RequestMapping("/stop")
    public void remove() throws ClassNotFoundException, SchedulerException {
        Scheduler sche = gSchedulerFactory.getScheduler();
        Task.removeJob(sche, "测试");
    }


    //可以用这种方式来验证,名为"测试",组为"JOB_GROUP_SYSTEM"的这个定时器是否存在.
//可以在接口中加入每次验证,并且加入到数据库中.
    @RequestMapping("/status")
    public void t2() throws SchedulerException, ClassNotFoundException {
        Scheduler sche = gSchedulerFactory.getScheduler();
        JobKey k = new JobKey("测试", "JOB_GROUP_SYSTEM");
        JobDetail jobDetail = sche.getJobDetail(k);
        if (jobDetail == null) {
            System.out.println("当前没有定时器");
        } else {
            System.out.println("有定时器在执行");
        }
    }
}
